package cn.usian.jyzx.product.service.impl;

import cn.usian.jyzx.model.entity.product.Brand;
import cn.usian.jyzx.product.mapper.BrandMapper;
import cn.usian.jyzx.product.service.BrandService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.annotations.SerializedName;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 06 19 10 44
 **/
@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

   @Autowired
   BrandMapper brandMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public List<Brand> findAll() {
        if(redisTemplate.hasKey("brandList")){
            String brandList = redisTemplate.opsForValue().get("brandList");
            log.info("redis缓存中获取品牌数据:{}",brandList);
            List<Brand> brands = JSONObject.parseArray(brandList, Brand.class);
            return brands;
        }else {
            List<Brand> brands = brandMapper.findAll();
            log.info("rmysql" +
                    "获取品牌数据:{}",brands);
            redisTemplate.opsForValue().set("brandList",JSONObject.toJSONString(brands));
            return brands;
        }

    }
}
